package com.it.web.action;


import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.delivery.Order;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order>{
	private static final long serialVersionUID = 1L;
	
	//属性注入：发送地址和收件地址
	private String sendAreaInfo;
	private String recAreaInfo;
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}



	//提交按钮点击触发
	@Action(value="order_add", results={@Result(name="success", type="redirect", location="index.html#/myhome")})
	public String add() {
		//封装Area
		Area sendArea = new Area();
		String[] sendAreaData = sendAreaInfo.split("/");
		if (sendAreaData != null && sendAreaData.length != 0) {
			sendArea.setProvince(sendAreaData[0]);
			sendArea.setCity(sendAreaData[1]);
			sendArea.setDistrict(sendAreaData[2]);
		}
		
		Area recArea = new Area();
		String[] recAreaData = recAreaInfo.split("/");
		if (recAreaData != null && recAreaData.length != 0) {
			recArea.setProvince(recAreaData[0]);
			recArea.setCity(recAreaData[1]);
			recArea.setDistrict(recAreaData[2]);
		}
		
		
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		
		//关联到当前关联客户
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		System.out.println("customer :" + customer);
		System.out.println("customer.getId()" + customer.getId());
		model.setCustomer_id(customer.getId());
		
		//调用webService将数据传递bos系统
		WebClient.create("http://localhost:9001/bos_management/services/orderService/order")
				.type(MediaType.APPLICATION_JSON).post(model);
		
		return SUCCESS;
	}

}
